﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using EFERP;
using HRM.DICTIONARY.DAL;

namespace HRM.DICTIONARY.UI
{
    public partial class xfrmWork : DevExpress.XtraEditors.XtraForm
    {
       private ICommon<hrm_work> _exeDic;
       private ICommon<hrm_employee> _exeDicEmployee;
       private ICommon<hrm_work_employee> _exeDicWorkEmployee;
        private hrm_work _currObj;
        private bool _isChooseRow;

        public xfrmWork()
        {
            InitializeComponent();
            this.Focus();
        }

        private void xfrmWork_Load(object sender, EventArgs e)
        {
            _exeDic = new Common<hrm_work>(StaticVariable.ERPEn);
            _exeDicEmployee = new Common<hrm_employee>(StaticVariable.ERPEn);
            _exeDicWorkEmployee = new Common<hrm_work_employee>(StaticVariable.ERPEn);

            _exeDic.GV = gbList;
            _exeDic.GC = gcList;
            _exeDic.ColIDName = "workid"; 
            Reload();
        }

        private void LoadDefault()
        {
            bbiFilter.EditValue = 0;
            bbeBeginDate.EditValue = DateTime.Today.AddDays(-15);
            bbeEndDate.EditValue = DateTime.Today.AddDays(15);
        }

        private void Add()
        {
            xfrmWorkAdd frm = new xfrmWorkAdd(_exeDic);
            frm.ShowDialog();
            if (_exeDic.myAction == Actions.Insert || _exeDic.myAction == Actions.InsertMultiple)
            {
                _exeDic.myAction = Actions.View;
            }
            Reload();
        }

        private void Edit()
        {
            xfrmWorkAdd frm = new xfrmWorkAdd(_currObj, _exeDic);
            frm.ShowDialog();
            if (_exeDic.myAction == Actions.Update)
            {
                _exeDic.myAction = Actions.View;Reload();
            }
            _isChooseRow = false;
            DoShowOrHide(_isChooseRow);
        }

        private void Delete()
        {
            bool kq = _exeDic.DeleteOnGridView(_currObj);
            if (kq)
            {
                _isChooseRow = false;
                DoShowOrHide(_isChooseRow);
            }
            else
            {
                MessageBox.Show("Không thể xóa dữ liệu");
            }
        }

        private void Reload()
        {
            if (_exeDic != null)
            {
                _exeDic.AddGridControlFromList();
                _isChooseRow = false;
                _currObj = null;
                DoShowOrHide(_isChooseRow);
            }
        }

        public void Print()
        {
            if (gbList != null)
            {
                if (gbList.RowCount > 0)
                {
                    gbList.ShowPrintPreview();
                    _isChooseRow = false;
                    _currObj = null;
                    DoShowOrHide(_isChooseRow);
                }
            }
        }

      

        public void Export()
        {
            if (_exeDic != null)
            {
                _exeDic.AddGridExport(this);
                _isChooseRow = false;
                _currObj = null;
                DoShowOrHide(_isChooseRow);
            }
        }

        public void SearchByDate(DateTime datefrom, DateTime dateto)
        {
            if (_exeDic != null)
            {
                List<hrm_work> listsearch = _exeDic.GetList().Where(s => (s.begindate >= datefrom && s.begindate <= dateto) || (s.enddate >= datefrom && s.enddate <= dateto)).ToList();
                _exeDic.AddGridControlFromList(listsearch);
                _isChooseRow = false;
                _currObj = null;
                DoShowOrHide(_isChooseRow);
            }
        }

        public void LoadDataEmployee(hrm_work objwork)
        {
            if (objwork == null) return;
            try
            {
                this.imglsEmployee.Items.Clear();
                hrm_employee objEmployee = _exeDicEmployee.GetById(objwork.employeecode);
                this.imglsEmployee.Items.Add(
                    objEmployee.firstname + " " + objEmployee.lastname + " (" + objEmployee.employeecode + ")",
                    objEmployee.sex == 1 ? 1 : 0);
                List<hrm_work_employee> listwWorkEmployees =
                    _exeDicWorkEmployee.GetList().Where(s => s.workid == objwork.workid).ToList();
                foreach (hrm_work_employee objWorkEmployee in listwWorkEmployees)
                {
                    objEmployee = _exeDicEmployee.GetById(objWorkEmployee.employeecode);
                    this.imglsEmployee.Items.Add(
                        objEmployee.firstname + " " + objEmployee.lastname + " (" + objEmployee.employeecode + ")",
                        objEmployee.sex == 1 ? 1 : 0);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        public void LoadDataInfo(hrm_work objwork)
        {
            if (objwork == null) return;
            try
            {
                this.mmDescription.Text = @"Công việc: " + Environment.NewLine + objwork.work + Environment.NewLine + Environment.NewLine + @"Ghi chú: " + Environment.NewLine + objwork.description;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void DoShowOrHide(bool isChoose)
        {
            bbiEdit.Enabled = isChoose;
            bbiDelete.Enabled = isChoose;
        }protected override bool ProcessDialogKey(Keys keyData)
        {
            switch (keyData)
            {
                case Keys.Escape:
                    Dispose();
                    return true;

                case Keys.F1:
                    //Help Link Click
                    return true;

                case Keys.F5:
                    this.Reload();
                    return true;

                case (Keys.Control | Keys.E):
                    this.Export();
                    return true;

                case (Keys.Control | Keys.F):
                    //Search
                    return true;

                case (Keys.Control | Keys.N):
                    Add();
                    return true;

                case (Keys.Enter):
                    Edit();
                    return true;

                case (Keys.Control | Keys.X):
                    Delete();
                    return true;

                case (Keys.Control | Keys.P):
                    this.Print();
                    return true;

                case (Keys.Alt | Keys.X):
                    Dispose();
                    return true;
            }
            return false;
        }

        #region Events

        private void bbiAdd_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            Add();
        }

        private void bbiEdit_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            Edit();
        }

        private void bbiDelete_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            Delete();
        }

        private void bbiRefresh_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            Reload();
        }

        private void bbiPrint_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            Print();
        }

        private void bbiExport_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            Export();
        }

        private void bbiClose_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            Dispose();
        }

        private void gbList_Click(object sender, EventArgs e)
        {
            _currObj = _exeDic.gbList_Click(sender, e);
            if (_currObj == null)
            {
                MessageBox.Show("Vui lòng chọn dòng trên bảng tính");
            }
            else
            {
                //load mo ta
                LoadDataInfo(_currObj);
                //load nhan vien lam cung cong viec
                LoadDataEmployee(_currObj);
                _isChooseRow = true;
                DoShowOrHide(_isChooseRow);
            }
        }

        private void gbList_DoubleClick(object sender, EventArgs e)
        {
            _currObj = _exeDic.gbList_Click(sender, e);
            if (_currObj == null)
            {
                MessageBox.Show("Vui lòng chọn dòng trên bảng tính");
            }
            else
            {Edit();
            }
        }

        public void xucDegree_KeyDown(object sender, KeyEventArgs e)
        {
            switch (e.KeyData)
            {
                case Keys.Escape:
                    Dispose();
                    break;

                case Keys.F1:
                    //Help Link Click

                case Keys.F5:
                    this.Reload();
                    break;

                case (Keys.Control | Keys.E):
                    this.Export();
                    break;

                case (Keys.Control | Keys.F):
                    //Search
                    break;

                case (Keys.Control | Keys.N):
                    //Call Form Add
                    break;

                case (Keys.Control | Keys.P):
                    this.Print();
                    break;

                case (Keys.Alt | Keys.D):
                    MessageBox.Show("You are master ");
                    break;

                case (Keys.Alt | Keys.X):
                    Dispose();
                    break;
            }
        }

        private void xucDegree_KeyUp(object sender, KeyEventArgs e)
        {

        }

        private void xucDegree_PreviewKeyDown(object sender, PreviewKeyDownEventArgs e)
        {

        }
        #endregion

        private void bbiSelect_Click(object sender, EventArgs e)
        {

        }
    }
}